/**
 * Copyright(C) 2008 Matthias Braunhofer
 * All rights reserved
 *
 * Created on: 28.07.2008
*/

package it.cums.wizards;

import it.cums.util.Converter;

import org.eclipse.jface.wizard.IWizardPage;
import org.eclipse.jface.wizard.WizardPage;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.ModifyEvent;
import org.eclipse.swt.events.ModifyListener;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Text;

public class ProductWizardPage extends WizardPage implements IWizardPage {
	private Text nameText;
	private Text manufacturerText;
	private Text purchasingPriceText;
	private Text salesPriceText;
	private Text ivaText;
	private Text availableQuantityText;
	private Text notesText;
	private Composite container;
	
	public ProductWizardPage() {
		super("New Product");
		setTitle("Create a New Product");
		setDescription("Enter a product name");
	}

	@Override
	public void createControl(Composite parent) {
		container = new Composite(parent, SWT.NULL);
		GridLayout layout = new GridLayout();
		container.setLayout(layout);
		layout.numColumns = 2;
		Label label1 = new Label(container, SWT.NULL);
		label1.setText("Product name:");
		
		nameText = new Text(container, SWT.BORDER | SWT.SINGLE);
		GridData gd = new GridData(GridData.FILL_HORIZONTAL);
		nameText.setLayoutData(gd);
		nameText.addModifyListener(new ModifyListener() {
			@Override
			public void modifyText(ModifyEvent arg0) {
				dialogChanged();
			}
        });
		
		Label label2 = new Label(container, SWT.NULL);
		label2.setText("Manufacturer:");
		
		manufacturerText = new Text(container, SWT.BORDER | SWT.SINGLE);
		manufacturerText.setLayoutData(gd);
		
		Label label3 = new Label(container, SWT.NULL);
		label3.setText("Available quantity:");
		
		availableQuantityText = new Text(container, SWT.BORDER | SWT.SINGLE);
		availableQuantityText.setLayoutData(gd);
		availableQuantityText.addModifyListener(new ModifyListener() {
			@Override
			public void modifyText(ModifyEvent arg0) {
				dialogChanged();
			}
        });
		
		Label label4 = new Label(container, SWT.NULL);
		label4.setText("Purchasing price (Euro):");
		
		purchasingPriceText = new Text(container, SWT.BORDER | SWT.SINGLE);
		purchasingPriceText.setLayoutData(gd);
		purchasingPriceText.addModifyListener(new ModifyListener() {
			@Override
			public void modifyText(ModifyEvent arg0) {
				dialogChanged();
			}
        });
		
		Label label5 = new Label(container, SWT.NULL);
		label5.setText("Iva (%):");
		
		ivaText = new Text(container, SWT.BORDER | SWT.SINGLE);
		ivaText.setLayoutData(gd);
		ivaText.addModifyListener(new ModifyListener() {
			@Override
			public void modifyText(ModifyEvent arg0) {
				dialogChanged();
			}
        });
		
		Label label6 = new Label(container, SWT.NULL);
		label6.setText("Sales price (Euro):");
		
		salesPriceText = new Text(container, SWT.BORDER | SWT.SINGLE);
		salesPriceText.setLayoutData(gd);
		salesPriceText.addModifyListener(new ModifyListener() {
			@Override
			public void modifyText(ModifyEvent arg0) {
				dialogChanged();
			}
        });
		
		Label label7 = new Label(container, SWT.NULL);
		label7.setText("Notes:");
		
		GridData gridData = new GridData(GridData.FILL_BOTH);
		gridData.verticalSpan = 2;
		notesText = new Text(container, SWT.BORDER | SWT.SINGLE);
		notesText.setLayoutData(gridData);
		notesText.addModifyListener(new ModifyListener() {
			@Override
			public void modifyText(ModifyEvent arg0) {
				dialogChanged();
			}
        });
		
		setControl(container);
		setPageComplete(false);
	}
	
	private void dialogChanged() {
		boolean complete = true;
		if (getName().length() == 0) {
			setDescription("Enter a product name");
			setErrorMessage(null);
			complete = false;
		} 
		if (complete && getAvailableQuantity().length() != 0) {
			try {
				Converter.convertToInteger(getAvailableQuantity());
				setErrorMessage(null);
			} catch (Exception e) {
				complete = false;
				setErrorMessage("Enter a valid quantity");
			}
		} 
		if (complete && getPurchasingPrice().length() != 0) {
			try {
				Converter.convertToDouble(getPurchasingPrice());
				setErrorMessage(null);
			} catch (Exception e) {
				complete = false;
				setErrorMessage("Enter a valid purchasing price");
			}
		} 
		if (complete && getIva().length() == 0) {
			complete = false;
			setDescription("Enter a Iva");
			setErrorMessage(null);
		}
		if (complete && getIva().length() != 0) {
			try {
				Converter.convertToDouble(getIva());
				setErrorMessage(null);
			} catch (Exception e) {
				complete = false;
				setErrorMessage("Enter a valid Iva");
			}
		}
		if (complete && getSalesPrice().length() == 0) {
			complete = false;
			setDescription("Enter a sales price");
			setErrorMessage(null);
		}
		if (complete && getSalesPrice().length() != 0) {
			try {
				Converter.convertToDouble(getSalesPrice());
				setErrorMessage(null);
			} catch (Exception e) {
				complete = false;
				setErrorMessage("Enter a valid sales price");
			}
		}
		if (complete) {
			setDescription(null);
			setErrorMessage(null);
		}
		setPageComplete(complete);
	}
	
	public String getName() {
		return nameText.getText().trim();
	}
	
	public String getSalesPrice() {
		return salesPriceText.getText().trim();
	}
	
	public String getPurchasingPrice() {
		return purchasingPriceText.getText().trim();
	}
	
	public String getAvailableQuantity() {
		return availableQuantityText.getText().trim();
	}
	
	public String getIva() {
		return ivaText.getText().trim();
	}
	
	public String getManufacturer() {
		return manufacturerText.getText().trim();
	}
	
	public String getNotes() {
		return notesText.getText().trim();
	}
	
	@Override
	public Control getControl() {
		return container;
	}
}
